package com.keldian.mobilesafe.activity;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.keldian.mobilesafe.R;
import com.keldian.mobilesafe.dao.AntivirsuDao;
import com.keldian.mobilesafe.utils.MD5Utils;

import java.util.List;

/**
 * Created by Administrator on 16-1-21.
 */
public class AntiVirusActivity extends Activity {
    protected static final int SCANING = 0;
    protected static final int FINISH = 2;
    private static final String TAG = "AntiVirusActivity";
    private ImageView iv_scan;
    private ProgressBar progressBar1;
    private PackageManager pm;
    private TextView tv_scan_status;
    private LinearLayout ll_container;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_anti_virus);
        iv_scan = (ImageView) findViewById(R.id.iv_scan);
        tv_scan_status = (TextView) findViewById(R.id.tv_scan_status);
        ll_container = (LinearLayout) findViewById(R.id.ll_container);
        RotateAnimation ra = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        ra.setDuration(1000);
        ra.setRepeatCount(Animation.INFINITE);
        iv_scan.setAnimation(ra);
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
//        progressBar1.setMax(100);
//        new Thread(){
//            @Override
//            public void run() {
//                for (int i=0; i<100; i++){
//                    try {
//                        Thread.sleep(100);
//                        progressBar1.setProgress(i);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//
//                }
//            }
//        }.start();

        scanVirus();


    }

    private void scanVirus() {
        pm = getPackageManager();
        tv_scan_status.setText("正在初始化8核杀毒引擎。。。");
        new Thread() {
            @Override
            public void run() {
                List<PackageInfo> infos = pm.getInstalledPackages(0);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                progressBar1.setMax(infos.size());
                int progress = 0;
                for (PackageInfo info : infos) {
                    String datadir = info.applicationInfo.dataDir;
                    String sourcedir = info.applicationInfo.sourceDir;

                    String fileMd5 = MD5Utils.getFileMd5(sourcedir);
                    ScanInfo scaninfo = new ScanInfo();
                    scaninfo.name = info.applicationInfo.loadLabel(pm).toString();
                    scaninfo.packname = info.packageName;
                    Log.i(TAG, scaninfo.packname + "-----" + fileMd5);

                    //查询md5信息，是否在病毒数据库里面存在。
//                    ZipFile zipFile = new ZipFile(file);
//                    zipFile.getEntry("");
                    //发现病毒
//扫描安全
                    scaninfo.isvirus = AntivirsuDao.isVirus(fileMd5);
                    Message msg = Message.obtain();
                    msg.obj = scaninfo;
                    msg.what = SCANING;
                    handler.sendMessage(msg);

                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    progress++;
                    progressBar1.setProgress(progress);
                }
                Message msg = Message.obtain();
                msg.what = FINISH;
                handler.sendMessage(msg);
            }
        }.start();


    }

    /**
     * 扫描信息的内部类
     */
    class ScanInfo {
        String packname;
        String name;
        boolean isvirus;
    }


    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SCANING:
                    ScanInfo scanInfo = (ScanInfo) msg.obj;
                    tv_scan_status.setText("正在扫描：" + scanInfo.name);
                    TextView tv = new TextView(getApplicationContext());
                    if (scanInfo.isvirus) {
                        tv.setTextColor(Color.RED);
                        tv.setText("发现病毒：" + scanInfo.name);
                    } else {
                        tv.setTextColor(Color.BLACK);
                        tv.setText("扫描安全：" + scanInfo.name);
                    }
                    ll_container.addView(tv, 0);
                    break;
                case FINISH:
                    tv_scan_status.setText("扫描完毕");
                    iv_scan.clearAnimation();
                    break;
            }

        }
    };


}
